package com.ruoyi.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.UserIsAdminType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.SysUserBillcash;
import com.ruoyi.system.service.ISysReportService;
import com.ruoyi.system.service.ISysUserBillcashService;
import com.ruoyi.system.vo.SysUserReportRes;
import com.ruoyi.system.vo.req.SysReportReq;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户资金流水记录Controller
 *
 * @author Ericcc
 * @date 2021-03-11
 */
@Controller
@RequestMapping("/system/report")
public class SysReportController extends BaseController
{
    private String prefix = "system/report";

    @Autowired
    private ISysReportService sysReportService;

    @RequiresPermissions("system:report:user")
    @GetMapping("/user")
    public String reportUser()
    {
        return prefix + "/user";
    }

    /**
     * 查询用户报表
     */
    @RequiresPermissions("system:report:user")
    @PostMapping("/userReport")
    @ResponseBody
    public TableDataInfo userReport(SysReportReq sysReportReq)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if (user.getUserType()== UserIsAdminType.COMMON.getCode()){
            return getDataTable(null);
        }else if(user.getUserType()== UserIsAdminType.AGENT.getCode()){
            Map map = new HashMap();
            map.put("parentId",user.getUserId());
            sysReportReq.setParams(map);
            List<SysUserReportRes> list = sysReportService.selectChidldUserReport(sysReportReq);
            return getDataTable(list);
        }else if(user.getUserType()== UserIsAdminType.HIGHAGENT.getCode()){
            Map map = new HashMap();
            map.put("parentId",user.getUserId());
            sysReportReq.setParams(map);
            List<SysUserReportRes> list = sysReportService.selectHighChidldUserReport(sysReportReq);
            return getDataTable(list);
        }
        List<SysUserReportRes> list = sysReportService.selectUserReport(sysReportReq);
        return getDataTable(list);
    }

    @RequiresPermissions("system:report:agent")
    @GetMapping("/agent")
    public String reportAgent()
    {
        return prefix + "/agent";
    }

    /**
     * 查询代理报表
     */
    @RequiresPermissions("system:report:agent")
    @PostMapping("/agentReport")
    @ResponseBody
    public TableDataInfo agentReport(SysReportReq sysReportReq)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if (user.getUserType()== UserIsAdminType.AGENT.getCode()){
            return getDataTable(null);
        }else if(user.getUserType()== UserIsAdminType.HIGHAGENT.getCode()){
            Map map = new HashMap();
            map.put("parentId",user.getUserId());
            sysReportReq.setParams(map);
            List<SysUserReportRes> list = sysReportService.selectChidldAgentReport(sysReportReq);
            return getDataTable(list);
        }
        List<SysUserReportRes> list = sysReportService.selectAgentReport(sysReportReq);
        return getDataTable(list);
    }

    /**
     * 导出用户资金流水记录列表
     */
    @RequiresPermissions("system:billcash:export")
    @Log(title = "用户资金流水记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysUserBillcash sysUserBillcash)
    {
//        List<SysUserBillcash> list = sysUserBillcashService.selectSysUserBillcashList(sysUserBillcash);
//        ExcelUtil<SysUserBillcash> util = new ExcelUtil<SysUserBillcash>(SysUserBillcash.class);
//        return util.exportExcel(list, "billcash");
        return null;
    }


}
